#include "stdafx.h"

#include <boost/bind.hpp>

#include "Parser.h"
#include "SearchManager.h"

int main(int argc, char** argv)
{
   std::string dataFile("data.txt");
   std::string caseFile("cases.txt");
   std::string outFile("result.txt");
   int searchPrecision = 4;
   int resultQuantity = 20;

   if (argc > 1)
   {
      try
      {
         dataFile = argv[1];
         if (argv[2])
            caseFile = argv[2];
         if (argv[3])
            outFile = argv[3];
         if (argv[4])
            searchPrecision=boost::lexical_cast<int>(argv[4]);
         if (argv[5])
            resultQuantity=boost::lexical_cast<int>(argv[5]);
      }
      catch (boost::bad_lexical_cast &)
      {
         std::cout << "Usage: SearchTool.exe <datafile> <casefile> <outfile> <precision> <quantity>" << std::endl;
         return 0;
      }
   }

   //initialize data
   //////////////////////////////////////////////////////////////////////////
   const Storage store = DataParser::Parse(dataFile, &Storage::Add);

   const PartsList parts = CaseParser::Parse(caseFile, &PartsList::push_back);
   
   //process requests
   //////////////////////////////////////////////////////////////////////////
   const SearchProperties prop(searchPrecision, resultQuantity);
   const SearchManager manager(prop, store);

   std::ofstream result;
   result.open(outFile.c_str());
   //bunch requests
   std::for_each(parts.begin(), parts.end(), 
      boost::bind(&SearchManager::PrintRequest, &manager, _1, boost::ref(result)));
   result.close();

   return 0;
}